package cn.kxm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.kxm.dao.PartMapper;
import cn.kxm.pojo.Part;
import cn.kxm.pojo.PartDetail;
import cn.kxm.pojo.PartType;
import cn.kxm.pojo.Storageinfo;
import cn.kxm.service.PartDetailService;
import cn.kxm.service.PartService;
import cn.kxm.service.StorageInfoService;
@Service("partServiceImpl")
public class PartServiceImpl implements PartService{
    @Autowired
    private PartMapper partMapper;
    @Autowired
    private PartDetailService partDetailService;
    @Autowired
    private StorageInfoService infoService;
    
	@Override
	public List<Part> getPartList(String partBrand, String partNo,
			String partName, Integer partTypeId, Integer pageNo,
			Integer pageSize) {
		Part part=new Part();
		part.setPartBrand(partBrand);
		part.setPartNo(partNo);
		part.setPartName(partName);
		part.setPartTypeId(partTypeId);
		return partMapper.getPartList(part, pageNo, pageSize);
	}

	@Override
	public int getPartCount(String partBrand, String partNo, String partName,
			Integer partTypeId) {
		Part part=new Part();
		part.setPartBrand(partBrand);
		part.setPartNo(partNo);
		part.setPartTypeId(partTypeId);
		part.setPartName(partName);
		return partMapper.getPartCount(part);
	}

	@Override
	public List<PartType> getPartTypeList() {
		return partMapper.getPartTypeList();
	}

	@Override
	public List<Map<String, Object>> getPartListByBillCode(String billCode) {
		return partMapper.getPartListByBillCode(billCode);
	}
	
	@Override
	public List<Map<String, Object>> getPartListByCodes(String[] codes) {
		List<Map<String, Object>> maps=partMapper.getPartListByCodes(codes);
		Integer a=1;
		Float p=new Float(0.00);
		for (Map<String, Object> map : maps) {
			map.put("amount", a);
			map.put("price", p);
		}
		return maps;
	}


	@Override
	public List<Map<String, Object>> getPartListAndCompanyName(String customName,Integer page,
    		Integer size,String size2,String partNo,String name,Integer type) {
		Integer currPage=(page-1)*size;
		return partMapper.getPartListAndCompanyName(customName, currPage, size, size2, partNo, name, type);
	}


	@Override
	public int getPartListCount(String size2,String partNo,String name,Integer type) {
		return partMapper.getPartListCount(size2, partNo, name, type);
	}
	
	@Override
	public List<Map<String, Object>> getByCode(String code) {
		return partMapper.getByCode(code);
	}
	@Override
	public List<Map<String, Object>> getPart_ib(String code) {
		return partMapper.getPart_ib(code);
	}

	@Override
	public List<Map<String, Object>> getPartDetailList(Integer page,
			Integer rows, Part part, String mold) {
		Integer currPage=(page-1)*rows;
		return partMapper.getPartDetailList(currPage, rows, part, mold);
	}

	@Override
	public int getPartDetailListCount(Part part, String mold) {
		return partMapper.getPartDetailListCount(part, mold);
	}

	@Override
	public boolean addPart(Part part,PartDetail detail,Storageinfo info) {
		if(partMapper.addPart(part)<=0){
			return false;
		}
		if(partDetailService.addPartDetail(detail)<=0){
			return false;
		}
		if(info!=null){
			if(infoService.addStorageInfoByInfow(info)<=0){
				return false;
			}	
		}
		return true;
	}

	@Override
	public Part getPartByCode(String code) {
		return partMapper.getPartByCode(code);
	}

	@Override
	public boolean modifyPartByPart(Part part, PartDetail detail) {
		if(partMapper.modifyPartByPart(part)<=0){return false;}
		if(partDetailService.updatePartDetailByDetail(detail)<=0){return false;}
		return true;
	}

	@Override
	public boolean delPartByCode(String code) {
		if(partDetailService.removePartDetailByPartCode(code)<=0){return false;}
		if(partMapper.delPartByCode(code)<=0){return false;}
		return true;
	}

}
